package com.rr.middle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhuangcanrun
 * @Date 2021/11/08/13:00
 * @description
 **/
/*
* https://leetcode-cn.com/problems/bulls-and-cows/
* */
public class GetHint {
    public String getHint(String secret, String guess) {
        int length = secret.length();
        int aNum = 0;
        int bNum = 0;
        List<Integer> checkNums = new ArrayList<Integer>();
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < length; i++) {
            int dig1 = Integer.parseInt(secret.substring(i, i + 1));
            int dig2 = Integer.parseInt(guess.substring(i, i + 1));
            if (dig1 == dig2) {
                aNum++;
            } else {
                checkNums.add(dig2);
                if (map.containsKey(dig1)) {
                    map.put(dig1, map.get(dig1) + 1);
                } else {
                    map.put(dig1, 1);
                }
            }
        }
        for (Integer checkNum : checkNums) {
            if (map.containsKey(checkNum) && map.get(checkNum) != 0) {
                bNum++;
                map.put(checkNum, map.get(checkNum) - 1);
            }
        }
        return aNum + "A" + bNum + "B";
    }

    public static void main(String[] args) {
        System.out.println(new GetHint().getHint("1807", "7810"));
    }
}
